<?php

namespace app\command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Webman\Console\Util;

class KllxsValidate extends Command
{
    protected static $defaultName = 'kllxs:validate';
    protected static $defaultDescription = '创建kllxs的validate tp的验证器';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('name', InputArgument::REQUIRED, 'Validate name');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument('name');
        $output->writeln("Make validate $name");
        // $suffix = config('app.controller_suffix', '');
        $name = str_replace('\\', '/', $name);
        // $routeName = $name;
        /* if ($suffix && !strpos($name, $suffix)) {
            $name .= $suffix;
        } */
        $name .= "Validate"; //追加后缀
        if (!($pos = strrpos($name, '/'))) {
            $name = ucfirst($name);
            $validate_str = Util::guessPath(app_path(), 'validate') ?: 'validate';
            $file = app_path() . "/$validate_str/$name.php";
            $namespace = $validate_str === 'validate' ? 'App\validate' : 'app\validate';
        } else {
            $name_str = substr($name, 0, $pos);
            if ($real_name_str = Util::guessPath(app_path(), $name_str)) {
                $name_str = $real_name_str;
            } else if ($real_section_name = Util::guessPath(app_path(), strstr($name_str, '/', true))) {
                $upper = strtolower($real_section_name[0]) !== $real_section_name[0];
            } else if ($real_base_validate = Util::guessPath(app_path(), 'validate')) {
                $upper = strtolower($real_base_validate[0]) !== $real_base_validate[0];
            }
            $upper = $upper ?? strtolower($name_str[0]) !== $name_str[0];
            if ($upper && !$real_name_str) {
                $name_str = preg_replace_callback('/\/([a-z])/', function ($matches) {
                    return '/' . strtoupper($matches[1]);
                }, ucfirst($name_str));
            }
            $path = "$name_str/" . ($upper ? 'Validate' : 'validate');
            $name = ucfirst(substr($name, $pos + 1));
            $file = app_path() . "/$path/$name.php";
            $namespace = str_replace('/', '\\', ($upper ? 'App/' : 'app/') . $path);
            // echo $name_str . "\n";
        }
        // $routeName = $name_str != "" ? ($name_str . "/" . $name) : $name;
        $this->createValidate($name, $namespace, $file);

        return self::SUCCESS;
    }

    /**
     * 创建控制器 function
     *
     * @param string $name
     * @param string $namespace
     * @param string $file
     * @return void
     */
    protected function createValidate(
        string $name,
        string $namespace,
        string $file
    ) {
        $path = pathinfo($file, PATHINFO_DIRNAME);
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        $validate_content = <<<EOF
<?php

namespace $namespace;

use app\common\Validate;

class $name extends Validate
{
    /**
     * 验证规则
     */
    protected \$rule = [

    ];
    /**
     * 提示消息
     */
    protected \$message = [

    ];
    /**
     * 验证场景
     */
    protected \$scene = [
        'add'  => [],
        'edit' => [],
    ];

}

EOF;
        file_put_contents($file, $validate_content);
    }
}
